package rexsee.communication;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public class RexseeSendTo implements JavascriptInterface {

	public static final String INTERFACE_NAME = "SendTo";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeSendTo(childBrowser);
	}

	public static final String ADDRESS_SPLITER = ",";

	private final Context mContext;
	protected final Browser mBrowser;
	public RexseeSendTo(Context context) {
		super();
		mContext = context;
		mBrowser = null;
	}
	public RexseeSendTo(Browser browser) {
		super();
		mContext = browser.getContext();
		mBrowser = browser;
	}

	protected boolean isValidPath(String path) {
		return true;
	}

	//JavaScript Interface

	public void sms(String phoneNumbers, String message) {
		Intent intent = new Intent(Intent.ACTION_SENDTO);
		intent.setData(Uri.parse("smsto:" + phoneNumbers));
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra("sms_body", message);
		mContext.startActivity(intent);
	}

	public void mms(String phoneNumbers, String subject, String message, String attachment) {
		if (!isValidPath(attachment)) {
			mBrowser.exception(getInterfaceName(), "Invalid path.");
			return;
		}
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra("address", phoneNumbers);
		intent.putExtra("compose_mode", false);
		intent.putExtra("exit_on_sent", true);
		intent.putExtra("subject", subject);
		intent.putExtra("sms_body", message);
		intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(attachment));
		intent.setType("image/*");
		intent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
		mContext.startActivity(intent);
	}

	public void mail(String address, String cc, String bcc, String subject, String content, String attachment, String packageName) {
		if (!isValidPath(attachment)) {
			mBrowser.exception(getInterfaceName(), "Invalid path.");
			return;
		}
		try {
			Intent intent = new Intent();
			intent.setAction(Intent.ACTION_SEND);
			intent.putExtra(Intent.EXTRA_EMAIL, address.split(ADDRESS_SPLITER));
			intent.putExtra(Intent.EXTRA_CC, cc.split(ADDRESS_SPLITER));
			intent.putExtra(Intent.EXTRA_BCC, bcc.split(ADDRESS_SPLITER));
			intent.putExtra(Intent.EXTRA_SUBJECT, subject);
			intent.putExtra(Intent.EXTRA_TEXT, content);
			intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(attachment));
			intent.setType("*/*");
			if (packageName != null && !packageName.equals("")) {
				intent.setPackage(packageName);
			}
			mContext.startActivity(intent);
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
		}
	}
	public void mail(String address, String cc, String bcc, String subject, String content, String attachment) {
		mail(address, cc, bcc, subject, content, attachment, "com.android.email");
	}
	public void gmail(String address, String cc, String bcc, String subject, String content, String attachment) {
		mail(address, cc, bcc, subject, content, attachment, "com.google.android.gm");
	}

}
